package com.trw.trp.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.trw.trp.bean.PropertyBean;
import com.utuan.log.Log;
import com.utuan.log.LogFactory;

public class SystemSettingDao extends AbstractDao {

	private static Log log = LogFactory.getLog(SystemSettingDao.class);

	private static final String SQL_GET_PROPS = "SELECT SP.PROPERTY_ID,SP.PROPERTY_NAME,SP.PROPERTY_VALUE FROM TRP.SYSTEM_PROPERTIES SP";
	private static final String SQL_UPDATE_PROP = "UPDATE TRP.SYSTEM_PROPERTIES SET PROPERTY_VALUE=? WHERE PROPERTY_ID=?";

	public List<PropertyBean> getProperties() {
		List<PropertyBean> propList = new ArrayList<PropertyBean>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(SQL_GET_PROPS);
			rs = ps.executeQuery();
			PropertyBean bean = null;
			while (rs.next()) {
				bean = new PropertyBean();
				bean.setId(rs.getLong(1));
				bean.setName(rs.getString(2));
				bean.setValue(rs.getString(3));
				propList.add(bean);
			}
			conn.commit();
		} catch (SQLException e) {
			log.warn(e);
			try {
				conn.rollback();
			} catch (SQLException e1) {
				log.warn(e1);
			}
		} finally {
			close(conn, rs, ps);
		}
		return propList;
	}

	public void updateProperties(List<PropertyBean> props) {
		if (props == null) {
			return;
		}
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(SQL_UPDATE_PROP);
			for (PropertyBean prop : props) {
				ps.setString(1, prop.getValue());
				ps.setLong(2, prop.getId());
				ps.execute();
			}
			conn.commit();
		} catch (SQLException e) {
			log.warn(e);
			try {
				conn.rollback();
			} catch (SQLException e1) {
				log.warn(e1);
			}
		} finally {
			close(conn, null, ps);
		}
	}

	public PropertyBean getProperty(long propId) {
		return null;
	}

}
